package org.ahuanlien.crm.web.controller;


import org.ahuanlien.crm.domain.Task;
import org.ahuanlien.crm.query.StaffManagementQueryObject;
import org.ahuanlien.crm.service.IEmployeeService;
import org.ahuanlien.crm.service.ITaskService;
import org.ahuanlien.crm.util.JSONResult;
import org.ahuanlien.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("task")
public class TaskController {

	@Autowired
	private ITaskService taskService;
	@Autowired
	private IEmployeeService employeeService;


	//打开页面和数据获取分开
	@RequestMapping("view")
	public String view() {
		return "task/view";
	}

	@RequestMapping("query")
	@ResponseBody
	public PageResult query(StaffManagementQueryObject qo) {
		PageResult result = taskService.query(qo);
		return result;
	}



	@RequestMapping("saveOrUpdate")
	@ResponseBody
	public JSONResult saveOrUpdate(Task task) {
		try {
			taskService.saveOrUpdate(task);
		} catch (Exception e) {
			e.printStackTrace();
			return JSONResult.mark("操作失败");
		}
		return JSONResult.success("操作成功");
	}


	@RequestMapping("changeState")
	@ResponseBody
	@RequiresPermissions(value = {"task:changeState", "任务状态设置"}, logical = Logical.OR)
	public JSONResult changeState(Long id) {
		try {
			taskService.changeState(id);
		} catch (Exception e) {
			e.printStackTrace();
			return JSONResult.mark("操作失败");
		}
		return JSONResult.success("操作成功");
	}

	@RequestMapping("selectAll")
	@ResponseBody
	public List<Task> selectAll(){
		return taskService.selectAll();
	}

	@RequestMapping("selectByPrimaryKey")
	@ResponseBody
	public Task selectByPrimaryKey(Long id){
		return taskService.selectByPrimaryKey(id);
	}

	@RequestMapping("exportXls")
	public void exportXls(HttpServletResponse response) throws IOException{
		//文件下载响应头
		response.setHeader("Content-disposition","attachment;filename=task.xls");
		//创建excel文件(workbook)
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建工作簿
        HSSFSheet sheet = workbook.createSheet("day01");
        //标题行
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("日期");
        row.createCell(1).setCellValue("姓名");
        row.createCell(2).setCellValue("部门");
        row.createCell(3).setCellValue("任务");
        row.createCell(4).setCellValue("内容");
        //查询任务数据
        List<Task> tasks = taskService.selectAll();
        for (int i = 0; i < tasks.size(); i++){
            Task task = tasks.get(i);
            row = sheet.createRow(i+1);
            row.createCell(0).setCellValue(task.getDate());
            row.createCell(1).setCellValue(task.getEmp().getUsername());
            row.createCell(2).setCellValue(task.getDept().getName());
            row.createCell(3).setCellValue(task.getSn());
            row.createCell(4).setCellValue(task.getContent());
        }
        //写入数据
        workbook.write(response.getOutputStream());
    }
}
